﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MacomberMapSystem.Common.Serialization;
using System.Xml;
using MacomberMapSystem.Common.Database;

namespace MacomberMapSystem.Common.Integration.Blackstart
{
    /// <summary>
    /// This class holds information on a blackstart corridor path
    /// </summary>
    public class MM_Blackstart_Corridor_Target: MM_Serializable
    {

        #region Variable declarations
        /// <summary>Our collection of blackstart corridor elements for the primary path</summary>        
        public MM_Blackstart_Corridor_Element[] Primary = new MM_Blackstart_Corridor_Element[0];
        
        /// <summary>Our collection of blackstart corridor elements for the primary path</summary>        
        public MM_Blackstart_Corridor_Element[] Secondary= new MM_Blackstart_Corridor_Element[0];

        /// <summary>The target of the blackstart path</summary>
        public String Target;

        /// <summary>The XML configuration for our element</summary>       
        public XmlElement xConfig;

        /// <summary>The description of our target</summary>
        public String Description;
        #endregion


        #region Initialization
        /// <summary>
        /// Initialize a new blackstart corridor
        /// </summary>
        /// <param name="xElem"></param>
        /// <param name="Model"></param>
        public MM_Blackstart_Corridor_Target(XmlElement xElem, MM_Database_Model Model)
            : base(xElem, Model)
        {
            this.xConfig = xElem;
            this.Target = xElem.Attributes["Name"].Value;
            foreach (string str in "Primary,Secondary".Split(','))
                if (xElem[str] != null)
                {
                    List<MM_Blackstart_Corridor_Element> Corridors = new List<MM_Blackstart_Corridor_Element>();
                    foreach (XmlNode xNode in xElem[str].ChildNodes)
                        if (xNode is XmlElement)
                            Corridors.Add(new MM_Blackstart_Corridor_Element(xNode as XmlElement, Model));
                    if (str == "Primary")
                        Primary = Corridors.ToArray();
                    else
                        Secondary = Corridors.ToArray();
                }
        }

     
        /// <summary>
        /// Assign a model's information to a blackstart corridor
        /// </summary>
        /// <param name="Model"></param>
        public void AssignModel(MM_Database_Model Model)
        {
            foreach (MM_Blackstart_Corridor_Element[] Elems in new MM_Blackstart_Corridor_Element[][] { Primary, Secondary })
                foreach (MM_Blackstart_Corridor_Element Elem in Elems)
                    Elem.AssignModel(Model);
        }
        #endregion
        /// <summary>
        /// Report the target name
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return Target;
        }
    }
}
